import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import {
  NzFormatEmitEvent,
  NzTreeComponent,
  NzTreeNode
} from 'ng-zorro-antd';
import { SystemConfigService } from '@core/services/system-config.service';

@Component({
  selector: 'app-role-mag',
  templateUrl: './role-mag.component.html',
  styleUrls: ['./role-mag.component.less']
})
export class RoleMagComponent implements OnInit {

  public myGroup: FormGroup;
  public total : any = 0;
  public pageIndex : any = 1;
  public pageSize : any = 10;
  public roleInfos : any;
  public roleName : any;
  public roleDetail : any = {
    name: '',
    sort: Number,
    remark: ''
  };
  public nodes : any;
  public treeData: any;
  public menuLists: any = {
    id: '',
    menuIds: []
  };
  public isVisible = false;
  public isConfirmLoading = false;
  public isVisible1 = false;
  public isConfirmLoading1 = false;

  constructor(private systemService: SystemConfigService, private fb: FormBuilder) { }

  //新增修改模态框
  showModal(id): void {
    this.isVisible = true;

    if(id == null||id == ''){
      this.roleDetail = {};
    }else{
      this.systemService.roleInfoById(id).then((data: any) => {
        this.roleDetail = data.data
      }, data => {
      });
    }
  }

  handleOk(): void {
    this.isConfirmLoading = true;
    let params = {
      id: this.roleDetail.id,
      name: this.roleDetail.name,
      remark: this.roleDetail.remark,
      sort: this.roleDetail.sort
    };
    console.log(params);
    this.systemService.roleInfoUpdate(params).then((data: any) => {
      this.ngOnInit();
      this.isVisible = false;
      this.isConfirmLoading = false;
    }, data => {
    })
  }

  handleCancel(): void {
    this.isVisible = false;
    this.isVisible1 = false;
  }

  expanded(item){
    if (item.children) {
      item.expanded = true;
      item.children.map((value: any) => {
        this.expanded(value)
      });
    } else {
      item.isLeaf = true;
    };
    return item
  }

  //权限设置模态框
  showModal1(id): void {
    this.isVisible1 = true;
    this.menuLists.id = id;

    this.systemService.getMenusTree(id).then(
      (response: any) => {
        const node = response.data;
        this.nodes = this.createTreeData(node);
        const r = this.nodes.map((item: any) => {
          return this.expanded(item);
        });
        console.log(this.nodes)
      },
      reject => {
        console.log(reject);
      }
    );
  }

  handleOk1(): void {
    console.log(this.menuLists);
    this.isConfirmLoading1 = true;
    this.systemService.menuConf(this.menuLists).then((data: any) => {
      this.isVisible1 = false;
      this.isConfirmLoading1 = false;
    }, data => {
    });


  }

  nzEvent(event: NzFormatEmitEvent): void {
    console.log(event);
    if(event.eventName == "check"){
      this.menuLists.menuIds = event.keys
    }
  }

  // 把菜单数据处理成树形结构
  createTreeData(data) {
    this.treeData = [];
    if (data.length > 0) {
      for (const n of data) {
        if (!n.parent) {
          const obj: any = {
            title: n.text,
            key: n.id,
            typeName: n.typeName,
            action: n.action,
            style: n.style,
            id: n.id,
            sort: n.sort,
            checked: n.state?n.state.selected: false
          };
          this.treeData.push(obj);
          if(n.state&&n.state.selected){
            this.menuLists.menuIds.push(n.id)
          }
        }
      }

      this.run(data, this.treeData);
    }
    return this.treeData;
  }

  run(resData, chiArr) {
    if (resData.length !== 0) {
      if (chiArr) {
        for (const value of chiArr) {
          for (const value2 of resData) {
            if (value.id === value2.parent) {
              const obj = {
                title: value2.text,
                parent: value2.parent,
                key: value2.id,
                typeName: value2.typeName,
                action: value2.action,
                style: value2.style,
                typeId: value2.typeId,
                id: value2.id,
                sort: value2.sort,
                checked: value2.state?value2.state.selected: false
              };
              if(value2.state&&value2.state.selected){
                this.menuLists.menuIds.push(value2.id)
              }
              if (!value.children) {
                value.children = [];
              }
              value.children.push(obj);
            }
          }
          this.run(resData, value.children);
        }
      }
    }
  }

  //删除操作
  showDel(id){
    this.systemService.roleInfoDel(id).then((data: any) => {
      if(this.total%10==1) {
        this.pageIndex--;
        if(this.pageIndex==0){
          this.pageIndex = 1;
        }
      }
      this.ngOnInit();
    }, data => {
    });
  }

  search(){
    this.pageIndex = 1;
    this.loadPage();
  }

  loadPage(){
    this.ngOnInit();
  }

  ngOnInit() {
    this.myGroup = this.fb.group({
      name: [ null, [ Validators.required ] ],
      sort: [ null ],
      remark: [ null ]
    });
    let params = {
      pageNum: this.pageIndex,
      pageSize: this.pageSize,
      roleName: this.roleName == null?'':this.roleName
    };

    this.systemService.getRoleInfo(params).then((data: any) => {
      this.roleInfos = data.data;
      this.total = data.recordsTotal;
      console.log(this.roleInfos)
    }, data => {
    });
  }

}
